<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子传父</title>
</head>
<body>
<div id="app">
    <h1>{{message}}</h1>
    <!--自定义事件传给父组件事件，事件默认如果没有参数就是传子组件参数-->
    <cpn v-on:btn-click="cpnClick"></cpn>
</div>
<template id="cpn">
    <div>
        <button v-for="(item,index) in categories" v-on:click="btnClick(item)">{{item.name}}</button>
    </div>
</template>
<script src="../../js/vue.js"></script>
<script>
    const cpnc = ({
        template: "#cpn",
        data() {
            return {
                categories: [
                    {id: 001, name: "热门推荐"},
                    {id: 002, name: "情趣计生"},
                    {id: 003, name: "电子数码"},
                    {id: 004, name: "图书用品"},
                ]
            }
        },
        methods: {
            btnClick(item) {
                /*
                * 子传父：第一个参数是自定义事件,第二个参数为数据变量
                * */
                this.$emit("btn-click", item);
            }
        }
    });
    Vue.component(
        'cpn', cpnc,
    );
    new Vue({
        el: "#app",
        data: {
            message: "你好，世界",
        },
        methods: {
            cpnClick(item) {
                console.log(item);
            }
        }
    })
</script>
</body>
</html>
